package com.milhouz.quizzdroid.bean;

import java.util.ArrayList;
import java.util.List;

import android.os.Parcel;
import android.os.Parcelable;

public class Game implements Parcelable {

	public static final int NB_TEAM_DEFAULT = 1;

	GameMode gameMode;
	boolean isSoundEnable = false;
	boolean isTextToSpeech = false;
	boolean isCountDown = false;
	long countDownTime = 0;

	List<Player> players = new ArrayList<Player>();

	int nbTotalPlayers = 1;
	int currentPlayerIndex = 0;

	public enum GameMode {
		SINGLE, MULTIPLAYER
	}

	public Game() {
	}

	public Game(final GameMode gameMode) {
		this.gameMode = gameMode;
	}

	public Game(final GameMode gameMode, final boolean isTextToSpeech, final boolean isCountDown, final long countDownTime, final int nbPlayers) {
		this.gameMode = gameMode;
		this.isTextToSpeech = isTextToSpeech;
		this.isCountDown = isCountDown;
		this.countDownTime = countDownTime;
		this.nbTotalPlayers = nbPlayers;
		initPlayers();
	}

	public void initPlayers() {
		players.clear();
		for (int i = 0; i < nbTotalPlayers; i++) {
			this.players.add(new Player(i + 1));
		}
	}

	public void setNextPlayer() {
		this.currentPlayerIndex++;
		if (this.currentPlayerIndex >= nbTotalPlayers) {
			this.currentPlayerIndex = 0;
		}
	}

	public Player getCurrentPlayer() {
		return players.get(this.currentPlayerIndex);
	}

	public GameMode getGameMode() {
		return gameMode;
	}

	public boolean isTextToSpeech() {
		return isTextToSpeech;
	}

	public boolean isCountDown() {
		return isCountDown;
	}

	public long getCountDownTime() {
		return countDownTime;
	}

	public int getNbPlayers() {
		return nbTotalPlayers;
	}

	public boolean isSoundEnable() {
		return isSoundEnable;
	}

	public void setSoundEnable(boolean isSoundEnable) {
		this.isSoundEnable = isSoundEnable;
	}

	public void setNbPlayers(int nbPlayers) {
		this.nbTotalPlayers = nbPlayers;
	}

	public Player[] getPlayersArray() {
		Player[] playersArray = new Player[players.size()];
		int i = 0;
		for (Player player : players) {
			playersArray[i++] = player;
		}
		return playersArray;
	}

	@Override
	public void writeToParcel(Parcel parcel, int flags) {
		parcel.writeString(gameMode == null ? null : gameMode.name());
		parcel.writeString(String.valueOf(isTextToSpeech));
		parcel.writeString(String.valueOf(isCountDown));
		parcel.writeLong(countDownTime);
		parcel.writeInt(nbTotalPlayers);
		parcel.writeList(players);
	}

	public static final Creator<Game> CREATOR = new Creator<Game>() {
		@SuppressWarnings("unchecked")
		@Override
		public Game createFromParcel(Parcel source) {
			Game g = new Game();
			g.gameMode = GameMode.valueOf(source.readString());
			g.isTextToSpeech = Boolean.valueOf(source.readString());
			g.isCountDown = Boolean.valueOf(source.readString());
			g.countDownTime = source.readLong();
			g.nbTotalPlayers = source.readInt();
			g.players = source.readArrayList(Player.class.getClassLoader());
			return g;
		}

		@Override
		public Game[] newArray(int arg0) {
			return null;
		}
	};

	@Override
	public int describeContents() {
		return 0;
	}

	public void setTextToSpeech(boolean isTextToSpeech) {
		this.isTextToSpeech = isTextToSpeech;
	}

	public void setCountDown(boolean isCountDown) {
		this.isCountDown = isCountDown;
	}

	public void setCountDownTime(long countDownTime) {
		this.countDownTime = countDownTime;
	}

	public void setNbTeams(int nbTeams) {
		this.nbTotalPlayers = nbTeams;
	}

	@Override
	public String toString() {
		return "Game [gameMode=" + gameMode + ", isSoundEnable=" + isSoundEnable + ", isTextToSpeech=" + isTextToSpeech + ", isCountDown=" + isCountDown
				+ ", countDownTime=" + countDownTime + ", players=" + players + ", nbTotalPlayers=" + nbTotalPlayers + ", currentPlayerIndex="
				+ currentPlayerIndex + "]";
	}

}